//
//  YXWebViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2018/1/2.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXWebViewController.h"
#import "YXHomeViewModel.h"
#import "YXBannerModel.h"
#import "YXMessageModel.h"
#import "YXNoticeHeaderView.h"
@interface YXWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic ,strong) YXNoticeHeaderView *noticeHeaderView;
@end

@implementation YXWebViewController

- (YXNoticeHeaderView *)noticeHeaderView {
    if (!_noticeHeaderView) {
        _noticeHeaderView = [[YXNoticeHeaderView alloc] initWithFrame:(CGRectMake(0, -150, kScreenWidth, 150))];
    }
    return _noticeHeaderView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
    if ([self.title isEqualToString:@"通知"] ) {
        [self requestDataNotice];
   
    }else {
        [self requestData];
    }
}

- (void)requestDataNotice {
    
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    //    http://121.196.210.134:8082/test_mpos/wx/getNewNoticeList.do
    NSString *urlStr = @"getNewNoticeByid.do";
    NSDictionary *parameter = @{@"sessionId":[YXUserInfoManager getUserInfo].sessionId,@"id":self.ID};
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        if (REQUESTDATASUCCESS) {
            YXMessageModel *model = [YXMessageModel mj_objectWithKeyValues:responseObject[@"body"]];
            self.noticeHeaderView.timeLab.text =  model.createTimeString;
            self.noticeHeaderView.contentLab.text = model.content;
       
             self.webView = [[UIWebView alloc] init];
            self.webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
            self.webView.backgroundColor = [UIColor clearColor];
            self.webView.opaque = NO;
            // 添加额外的滚动附近区域的内容
            _webView.scrollView.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
            
            [_webView.scrollView addSubview:self.noticeHeaderView];
            // 自动对页面进行缩放以适应屏幕
            _webView.scalesPageToFit = YES;
            [self.view addSubview: self.webView];
            // 创建url
            NSURL *url = [NSURL URLWithString:model.url];
            // 请求url
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            // webview加载url
            [_webView loadRequest:request];

        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)requestData {
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [YXHomeViewModel getQuerySelectBannerListCategory:@"1" Type:@(self.type).stringValue ModelList:^(NSArray *listArr,YXBannerModel *model) {
        NSLog(@"%@",model.imgUrl);
        [self loadSubViews:model.imgUrl];
        [SVProgressHUD dismiss];
    } Failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

- (void)loadSubViews:(NSString *)urlStr {
    
    self.webView = [[UIWebView alloc] init];
    [UIView animateKeyframesWithDuration:2.0 delay:1.0 options:(UIViewKeyframeAnimationOptionOverrideInheritedDuration) animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
            self.webView.frame = CGRectMake(0, 200, 0,0);
            
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1.0 animations:^{
            self.webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
        }];
        
        // 自动对页面进行缩放以适应屏幕
        _webView.scalesPageToFit = YES;
        
        [self.view addSubview:_webView];
        
        // 创建url
        NSURL *url = [NSURL URLWithString:urlStr];
        
        // 请求url
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // webview加载url
        [_webView loadRequest:request];
        
    } completion:^(BOOL finished) {
        //NSLog(@"完成");
        
    }];
    
}



@end
